//package com.qf.mybatisplus.function.dingding;
//
//import com.dingtalk.api.DefaultDingTalkClient;
//import com.dingtalk.api.DingTalkClient;
//import com.dingtalk.api.request.*;
//import com.dingtalk.api.response.*;
//import com.taobao.api.ApiException;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.stereotype.Service;
//
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
///**
// *@author:
// *@date:
// *@desc: 钉钉发送工作通知
// */
//@Slf4j
//public class DingWorkBiz {
//
//    private String APP_KEY = "ding14ci3uxgdisf1mt7";
//    private String APP_SECRET = "sr7mSCUwLfJGSSlKQpor11du-5qCnIEMEicV6-rnRKgWayo34aRrSOihzo35Grfi";
//    private Long AGENT_ID = 1388513699L;
//
//    private String MESSAGE_URL = "https://www.baidu.com";
//    private String PC_MESSAGE_URL = "https://www.baidu.com";
//    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
//
//    /**
//     * 获取AccessToken
//     * @return  AccessToken
//     * @throws ApiException
//     */
//    private String getAccessToken() throws ApiException {
//        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
//        OapiGettokenRequest request = new OapiGettokenRequest();
//        //Appkey
//        request.setAppkey(APP_KEY);
//        //Appsecret
//        request.setAppsecret(APP_SECRET);
//        /*请求方式*/
//        request.setHttpMethod("GET");
//        OapiGettokenResponse response = client.execute(request);
//        return response.getAccessToken();
//    }
//
//    /**
//     * 发送OA消息
//     * @param mobile 发送消息人的电话，多个英文逗号拼接
//     * @throws ApiException
//     */
//    public void sendOA(String mobile) throws ApiException {
//        log.info("发送钉钉通知");
//        String accessToken = getAccessToken();
//        if(StringUtils.isBlank(mobile)){
//            return;
//        }
//        //电话号码数组
//        String[] split = mobile.split(",");
//        for (String s : split) {
//            DingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
//            OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
//            req.setMobile(s);
//            req.setHttpMethod("GET");
//            OapiUserGetByMobileResponse rsp = client2.execute(req, accessToken);
//            //获取到Urid就是在公司里要发送到那个人的id
//            String urid = rsp.getUserid();
//            //根据用户id获取用户详情
//            DingTalkClient userDetail = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get");
//            OapiUserGetRequest userReq = new OapiUserGetRequest();
//            userReq.setUserid(urid);
//            userReq.setHttpMethod("GET");
//            OapiUserGetResponse userRsp = userDetail.execute(userReq, accessToken);
//            String userName = userRsp.getName();
//
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
//            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
//            request.setUseridList(urid);
//            request.setAgentId(AGENT_ID);
//            request.setToAllUser(false);
//
//            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
//            msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
//            //跳转链接
//            msg.getOa().setMessageUrl(MESSAGE_URL);
//            msg.getOa().setPcMessageUrl(PC_MESSAGE_URL);
//            //设置head
//            msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
//            msg.getOa().getHead().setText("待办事宜");
//            msg.getOa().getHead().setBgcolor("00409eff");
//            //设置body
//            msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
//            msg.getOa().getBody().setTitle("邮件现有功能已完成开发，抄送、密送以及发送异常处理暂未开始~！");
//            msg.getOa().getBody().setContent("创建人：" + userName + "\n创建时间：" + sdf.format(new Date()));
//            //消息类型
//            msg.setMsgtype("oa");
//            request.setMsg(msg);
//            log.info("获取发送通知消息体和获取发送通知人完成");
//            OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request,accessToken);
//            log.info("发送消息是否成功"+response.isSuccess());
//            System.out.println(response.isSuccess());
//            log.info("消息任务ID"+response.getTaskId());
//            System.out.println(response.getTaskId());
//        }
//    }
//    /**
//     * 发送待办
//     * @param mobile 发送消息人的电话，多个英文逗号拼接
//     * @throws ApiException
//     */
//    public  void sendTask(String mobile) throws ApiException {
//        log.info("发送钉钉通知");
//        String accessToken = getAccessToken();
//        if(StringUtils.isBlank(mobile)){
//            return;
//        }
//        //电话号码数组
//        String[] split = mobile.split(",");
//        for (String s : split) {
//            //根据电话号码获取发送人id
//            DingTalkClient mobilClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
//            OapiUserGetByMobileRequest useReq = new OapiUserGetByMobileRequest();
//            useReq.setMobile(s);
//            useReq.setHttpMethod("GET");
//            OapiUserGetByMobileResponse useRsp = mobilClient.execute(useReq, accessToken);
//            //获取到Urid就是在公司里要发送到那个人的id
//            String urid = useRsp.getUserid();
//            OapiWorkrecordAddRequest req = new OapiWorkrecordAddRequest();
//
//            req.setUserid(urid);
//            req.setCreateTime(new Date().getTime());
//            req.setTitle("测试待办任务发送");
//            req.setUrl("https://oa.dingtalk.com");
//            req.setPcUrl(PC_MESSAGE_URL);
//            List<OapiWorkrecordAddRequest.FormItemVo> list2 = new ArrayList<>();
//            OapiWorkrecordAddRequest.FormItemVo obj3 = new OapiWorkrecordAddRequest.FormItemVo();
//            list2.add(obj3);
//            obj3.setTitle("新人学习，必须完成");
//            obj3.setContent("今日学习强国，你完成了吗？");
//
//            req.setFormItemList(list2);
//            //发起人id
//            req.setOriginatorUserId(urid);
//            req.setSourceName("学习~");
//            req.setPcOpenType(2L);
//            //流程业务id，避免多个业务冲突
//            req.setBizId(UUID.randomUUID().toString());
//            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/workrecord/add");
//            OapiWorkrecordAddResponse rsp = client.execute(req, accessToken);
//
//            System.out.println(rsp.getBody());
//        }
//    }
//
//    public static void main(String[] args) {
//
//        DingWorkBiz dingWorkBiz = new DingWorkBiz();
//        try {
////            dingWorkBiz.sendTask("13213018164");
//            dingWorkBiz.sendOA("13213018164");
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }
//    }
//
//}